package com.ke.searchhouse.service.impl;

import com.ke.searchhouse.dto.UserDTO;
import com.ke.searchhouse.entity.Role;
import com.ke.searchhouse.entity.User;
import com.ke.searchhouse.repository.RoleRepository;
import com.ke.searchhouse.repository.UserRepository;
import com.ke.searchhouse.service.IUserService;
import com.ke.searchhouse.util.ServiceResult;
import org.springframework.beans.BeanUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.security.authentication.DisabledException;
import org.springframework.security.core.GrantedAuthority;
import org.springframework.security.core.authority.SimpleGrantedAuthority;
import org.springframework.stereotype.Service;

import javax.transaction.Transactional;
import java.util.ArrayList;
import java.util.Arrays;
import java.util.Date;
import java.util.List;

/**
 * @author  keke
 * @create  2018/10/10 0010 下午 9:56
 * @desc 用户服务实现类
 **/
@Service
public class UserServiceImpl implements IUserService{
    @Autowired
    private UserRepository userRepository;
    @Autowired
    private RoleRepository roleRepository;

    @Override
    public User findUserByName(String userName) {
        User user = userRepository.findByName(userName);

        if (user == null) {
            return null;
        }

        List<Role> roles = roleRepository.findRolesByUserId(user.getId());
        if (null == roles || roles.isEmpty()) {
            throw new DisabledException("权限非法");
        }

        List<GrantedAuthority> authorities = new ArrayList<>();
        roles.forEach(role -> authorities.add(new SimpleGrantedAuthority("ROLE_" + role.getName())));
        user.setAuthorityList(authorities);

        return user;
    }

    @Override
    public ServiceResult<UserDTO> findById(Long adminId) {
        User user = userRepository.findById(adminId).orElse(null);

        if (user == null) {
            return ServiceResult.notFound();
        }

        UserDTO userDTO = new UserDTO();
        BeanUtils.copyProperties(user, userDTO);

        return ServiceResult.of(userDTO);
    }

    @Override
    public User findUserByTelephone(String telephone) {
        User user = userRepository.findUserByPhoneNumber(telephone);

        if (user == null) {
            return null;
        }

        List<Role> roles = roleRepository.findRolesByUserId(user.getId());
        if (roles == null || roles.size() == 0) {
            throw  new DisabledException("权限非法");
        }

        List<GrantedAuthority> authorities = new ArrayList<>();
        roles.forEach(role -> authorities.add(new SimpleGrantedAuthority("ROLE_" + role.getName())));

        user.setAuthorityList(authorities);

        return user;
    }

    @Override
    @Transactional(rollbackOn = Exception.class)
    public User addUserByPhone(String telephone) {
        User user = new User();
        user.setPhoneNumber(telephone);
        user.setName(telephone.substring(0, 3) + "****" + telephone.substring(7, telephone.length()));
        Date now = new Date();
        user.setCreateTime(now);
        user.setLastUpdateTime(now);
        user.setLastLoginTime(now);
        userRepository.save(user);

        Role role = new Role();
        role.setUserId(user.getId());
        role.setName("USER");
        roleRepository.save(role);

        user.setAuthorityList(Arrays.asList(new SimpleGrantedAuthority("ROLE_USER")));

        return user;
    }
}
